package com.lb.shopguide.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.GuideAdBean;
import com.lb.shopguide.entity.UpdateInfoBean;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.im.Preference.Preferences;
import com.lb.shopguide.ui.activity.GuideActivity;
import com.lb.shopguide.util.json.JsonUtil;

import butterknife.BindView;

/**
 * Created by kang on 2017/5/27.
 */

public class FragmentGuide extends BaseCommonFragment {
    @BindView(R.id.iv_back)
    ImageView ivBack;

    private UpdateInfoBean updateInfoBean;
    private GuideAdBean guideAdBean;

//    private boolean update_flag = false;//更新数据是否加载完全

    public static FragmentGuide newInstance() {
        FragmentGuide fragment = new FragmentGuide();
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_guide;
    }

    @Override
    public void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
    }

    @Override
    public void initData() {
        mUserConfigManager = UserConfigManager.getInstance(mContext);
        sendRequestGetAdLoading();
        sendRequestGetAppVersion();
        saveLoginInfo(mUserConfigManager.getAccId(), mUserConfigManager.getImToken());
    }

    @Override
    public void initView() {

    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
        initProcess();
    }

    @Override
    public String getPageTag() {
        return FragmentGuide.class.getName();
    }

    private void sendRequestGetAppVersion() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    if (response.getReturnContent() != null) {
                        updateInfoBean = (UpdateInfoBean) JsonUtil.getObject(response.getReturnContent(), UpdateInfoBean.class);
                        initProcess();
                    } else {
                        initProcess();
                    }
                }
            }
        };
        ApiMethodGuide.getAppVersion(new SimpleLbObserver(listener, otherListener), AppConstant.ANDROID_APP_TYPE, "s");
    }

    private void sendRequestGetAdLoading() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    if (response.getReturnContent() != null) {
                        guideAdBean = (GuideAdBean) JsonUtil.getObject(response.getReturnContent(), GuideAdBean.class);
                    }
                }
            }
        };
        ApiMethodGuide.getAdLoading(new SimpleLbObserver(listener, otherListener));
    }

    private void saveLoginInfo(final String account, final String token) {
        Preferences.saveUserAccount(account);
        Preferences.saveUserToken(token);
    }

    private void initProcess() {
        if (ivBack != null) {
            ivBack.postDelayed(new Runnable() {
                @Override
                public void run() {
                    jump();
                }
            }, 800);
        }
    }


    private void jump() {
        if (guideAdBean != null) {
            startWithPop(FragmentAd.newInstance(updateInfoBean, guideAdBean));
            return;
        }
        if (!TextUtils.isEmpty(mUserConfigManager.getUserId())) {
            Intent intent = null;
            if (mUserConfigManager.getTerminalType() == AppConstant.TERMINAL_TYPE_GUIDE) {
                intent = ((GuideActivity) _mActivity).getGuideMainActivityIntent();
            } else if (mUserConfigManager.getTerminalType() == AppConstant.TERMINAL_TYPE_BOSS) {
                intent = ((GuideActivity) _mActivity).getBossMainActivityIntent();
            } else if (mUserConfigManager.getTerminalType() == -1) {//之前的导购端，不存在这个字段，直接进入导购端
                mUserConfigManager.setTerminalType(AppConstant.TERMINAL_TYPE_GUIDE);
                intent = ((GuideActivity) _mActivity).getGuideMainActivityIntent();
            }
            if (updateInfoBean != null && intent != null) {
                intent.putExtra(AppConstant.UPDATE_BEAN, updateInfoBean);
            }
            if(!isDetached()){
                startActivity(intent);
                _mActivity.finish();
            }
        } else {
//            startWithPop(FragmentEnterPhone.newInstance(updateInfoBean));// FragmentManager is already executing transactions
            startWithPop(FragmentChooseRole.newInstance(updateInfoBean));
        }
    }
}
